package com.chookss.video;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.aliyun.apsara.alivclittlevideo.view.video.LoadingView;
import com.aliyun.apsara.alivclittlevideo.view.video.OnStsInfoExpiredListener;
import com.aliyun.apsara.alivclittlevideo.view.video.videolist.OnTimeExpiredErrorListener;
import com.aliyun.common.utils.DensityUtil;
import com.aliyun.player.IPlayer;
import com.aliyun.svideo.base.widget.CircleProgressBar;
import com.chookss.video.model.IVideoSourceModel;
import com.chookss.video.model.VideoSourceModel;
import com.johnrambo.ktea.common.klogutil.KLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 播放界面, 负责initPlayerSDK以及各种view
 *
 * @author xlx
 */
public class AlivcVideoPlayView extends FrameLayout {


    private static final String TAG = "AlivcVideoPlayView";
    private Context context;
    public AlivcVideoListView videoListView;

    /**
     * 刷新数据listener (下拉刷新和上拉加载)
     */
    private AlivcVideoListView.OnRefreshDataListener onRefreshDataListener;
    /**
     * 视频缓冲加载view
     */
    private LoadingView mLoadingView;


    /**
     * 分享选择提示窗
     *
     * @param context
     */

//    private ShareDialog mShareDialog;

    private LittleVideoListAdapter.OnItemBtnClick onItemBtnClick;


    /**
     * 视频删除点击事件
     */
    private LittleVideoListAdapter mVideoAdapter;

    public AlivcVideoPlayView(@NonNull Context context) {
        this(context, null);
    }

    public AlivcVideoPlayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initVideo();
    }

    private void initVideo() {
        initPlayListView();
        initLoadingView();
    }
    private CircleProgressBar mProgressBar;
    private TextView mTvProgress;

    public void setOnItemBtnClick(LittleVideoListAdapter.OnItemBtnClick onItemBtnClick) {
        this.onItemBtnClick = onItemBtnClick;
        if (mVideoAdapter != null) {
            mVideoAdapter.setItemBtnClick(onItemBtnClick);
        }
    }

    public LittleVideoListAdapter getVideoAdapter() {
        return mVideoAdapter;
    }
   public void emptyView(String type){

   }
    private int mClickPosition;

    /**
     * 初始化视频列表
     */
    private void initPlayListView() {
        videoListView = new AlivcVideoListView(context);
        //创建adapter，需要继承BaseVideoListAdapter
        mVideoAdapter = new LittleVideoListAdapter(context);
        mVideoAdapter.setItemBtnClick(onItemBtnClick);
        //给AlivcVideoListView实例化对象添加adapter
        videoListView.setAdapter(mVideoAdapter);
        videoListView.setVisibility(VISIBLE);
        //设置下拉、上拉监听进行加载数据
        videoListView.setOnRefreshDataListener(new AlivcVideoListView.OnRefreshDataListener() {
            @Override
            public void onRefresh() {
                if (onRefreshDataListener != null) {
                    onRefreshDataListener.onRefresh();
                }
            }

            @Override
            public void onLoadMore() {
                if (onRefreshDataListener != null) {
                    onRefreshDataListener.onLoadMore();
                }
            }
        });
        //设置视频缓冲监听
        videoListView.setLoadingListener(new IPlayer.OnLoadingStatusListener() {
            @Override
            public void onLoadingBegin() {
                if(mLoadingView!=null) {
                    mLoadingView.start();
                }
            }

            @Override
            public void onLoadingEnd() {
                if(mLoadingView!=null) {
                    mLoadingView.cancle();
                }
            }

            @Override
            public void onLoadingProgress(int var1, float var2) {

            }
        });
        //设置鉴权过期监听，刷新鉴权信息
        videoListView.setTimeExpiredErrorListener(new OnTimeExpiredErrorListener() {

            @Override
            public void onTimeExpiredError() {
                if (mStsInfoExpiredListener != null) {
                    mStsInfoExpiredListener.onTimeExpired();
                }
            }
        });
        //添加到布局中
        addSubView(videoListView);
    }

    /**
     * 播放、下载、上传过程中鉴权过期时提供的回调消息
     */
    private OnStsInfoExpiredListener mStsInfoExpiredListener;

    public void setOnStsInfoExpiredListener(
        OnStsInfoExpiredListener mTimeExpiredErrorListener) {
        this.mStsInfoExpiredListener = mTimeExpiredErrorListener;
    }

    private void initLoadingView() {
        mLoadingView = new LoadingView(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                5);
        params.setMargins(0, 0, 0, DensityUtil.dip2px(getContext(), 4));
        params.gravity = Gravity.BOTTOM;
        addView(mLoadingView, params);
    }

    /**
     * addSubView 添加子view到布局中
     *
     * @param view 子view
     */
    private void addSubView(View view) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        addView(view, params);
    }

    /**
     * 刷新视频列表数据
     *
     * @param datas
     */
    public void refreshVideoList(List<? extends VideoSourceModel> datas, boolean isEnd) {
        List<IVideoSourceModel> videoList = new ArrayList<>();
        videoList.addAll(datas);
        if(videoListView!=null) {
            videoListView.refreshData(videoList, isEnd);
        }
        //取消加载loading
        if(mLoadingView!=null) {
            mLoadingView.cancle();
        }
    }

    public void completeLoadState(boolean isEnd) {
        if(videoListView!=null) {
            videoListView.completeLoadState(isEnd);
        }
        //取消加载loading
        if(mLoadingView!=null) {
            mLoadingView.cancle();
        }
    }

    /**
     * 刷新视频列表数据
     *
     * @param datas
     * @param position
     */
    public void refreshVideoList(List<VideoSourceModel> datas, int position, boolean isEnd) {
        List<IVideoSourceModel> videoList = new ArrayList<>();
        videoList.addAll(datas);
        if(videoListView!=null) {
            videoListView.refreshData(videoList, position, isEnd);
        }
        //取消加载loading
        if(mLoadingView!=null) {
            mLoadingView.cancle();
        }
    }

    public void scrollToPosition(int position) {
        if(videoListView!=null) {
            videoListView.scrollToPosition(position);
        }
    }



    /**
     * 添加更多视频
     *
     * @param datas
     */
    public void addMoreData(List<? extends VideoSourceModel> datas, boolean isEnd) {
        List<IVideoSourceModel> videoList = new ArrayList<>();
        videoList.addAll(datas);
        if(videoListView!=null) {
            videoListView.addMoreData(videoList, isEnd);
        }
        //取消加载loading
        if(mLoadingView!=null) {
            mLoadingView.cancle();
        }
    }

    /**
     * 设置下拉刷新数据listener
     *
     * @param listener OnRefreshDataListener
     */
    public void setOnRefreshDataListener(AlivcVideoListView.OnRefreshDataListener listener) {
        this.onRefreshDataListener = listener;
    }

    public void onStart() {

    }

    public void onResume() {
        if(videoListView!=null) {
            videoListView.setOnBackground(false);
        }
    }

    public void onStop() {
        if(mLoadingView!=null){
            mLoadingView.cancle();
        }

    }

    public void onPause() {

        if(videoListView!=null) {
            videoListView.setOnBackground(true);
        }
    }

    public void onDestroy() {
//        context = null;
        if(videoListView!=null){
            videoListView.onDestroy();
        }
    }

    /**
     * 视频列表获取失败
     */
    public void loadFailure() {
        if(mLoadingView!=null) {
            mLoadingView.cancle();
        }
        if(videoListView!=null) {
            videoListView.loadFailure();
        }
    }


    private FragmentActivity mActivity;

    private FragmentManager getFragmentManager() {
        FragmentManager fm = null;
        if (mActivity != null) {
            fm = mActivity.getSupportFragmentManager();
        } else {
            Context mContext = getContext();
            if (mContext instanceof FragmentActivity) {
                fm = ((FragmentActivity)mContext).getSupportFragmentManager();
            }
        }
        return fm;
    }

}
